﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using WindowsUserControl.Codes;

namespace Workflows.Components.Rules
{
    public partial class CodeRuleConditionEditor : Form
    {
        private CodeFragment _Editor;
        public event RuleDataUpdated OnRuleConditionUpdated;
        private object _OwnerRuleNode;
        private Rule _Rule;
        private CodeRuleCondition _CodeRule;

        /// <summary>
        /// bind to external assembly/class/method to evaluate
        /// </summary>
        public CodeRuleConditionEditor()
        {
            InitializeComponent();
            this.groupBox1.Controls.Clear();
            this._Editor = new CodeFragment(string.Empty,string.Empty,new List<string>());
            this.groupBox1.Controls.Add(this._Editor);
            this._Editor.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// bind rule node data
        /// </summary>
        /// <param name="ownerRuleNode"></param>
        /// <param name="rule"></param>
        /// <param name="condition"></param>
        public void InitRuleCondition(object ownerRuleNode, Rule rule, CodeRuleCondition condition)
        {
            this._OwnerRuleNode = ownerRuleNode;
            this._Rule = rule;
            this._CodeRule = condition;
            this.DisplaySettings();
        }


        /// <summary>
        /// 
        /// </summary>
        public void DisplaySettings()
        {
            List<string> assemblyFilePaths=new List<string>();
            if (this._Rule != null && !string.IsNullOrEmpty(this._Rule.AssemblyFilePath))
            {
                assemblyFilePaths.Add(this._Rule.AssemblyFilePath);
            }
            // this._Editor.SetCodeReferences(assemblyFilePaths);
            try
            {
                RuleParser parser = new RuleParser(this._Rule);
                string classCode = parser.ClassCodeTemplate;
                string placeHolder = string.Format(" // @{0}", this._CodeRule.RuleNodeID.ToString());
                string headerCode = classCode.Substring(0, classCode.LastIndexOf("}"));
                headerCode = headerCode.Substring(0, headerCode.LastIndexOf("}"));
                string footerCode = "}\n}\n";
                
                if(classCode.IndexOf(placeHolder)>0)
                {
                    headerCode = classCode.Substring(0, classCode.IndexOf(placeHolder));
                    footerCode = classCode.Substring(classCode.IndexOf(placeHolder) + placeHolder.Length);
                }
                this._Editor.HeaderCode = headerCode;
                this._Editor.FooterCode = footerCode;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            if (!string.IsNullOrEmpty(this._CodeRule.CodeSnippet))
            {
                this._Editor.CodeSnippet = this._CodeRule.CodeSnippet;
            }
            else
            {
                this._Editor.CodeSnippet = "";
            }
        }

        private void CodeRuleConditionEditor_Load(object sender, EventArgs e)
        {

        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this._CodeRule.CodeSnippet = this._Editor.CodeSnippet;

            if(this.OnRuleConditionUpdated !=null)
            {
                this.OnRuleConditionUpdated(this._OwnerRuleNode, this._Rule, this._CodeRule);
            }

            this._Editor.Dispose();
            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this._Editor.Dispose();
            this.Close();
        }
    }
}
